//https://leetcode.cn/problems/maximum-subarray/
package codeRandomThoughts.Test53最大子序和;

//动态规划
public class Solution2 {
    public int maxSubArray(int[] nums) {
        int max = nums[0];
        //dp[i]:以nums[i]为结尾的连续子数组最大和
        int[] dp = new int[nums.length];
        dp[0] = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (dp[i - 1] > 0) {
                dp[i] = dp[i - 1] + nums[i];
            } else {
                dp[i] = nums[i];
            }

            if (dp[i] > max) {
                max = dp[i];
            }
        }

        return max;
    }
}
